package com.newland.nxsms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.newland.nxsms.utils.Constants;
import com.newland.nxsms.utils.DbUtils;

/**
 * @author skylicun
 * @version V1.0
 * @Description AbstractDao
 */
public abstract class AbstractDao
{
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	public int updateQuietly(Connection conn, boolean closeConn, String sql, Object... params)
	{
		try
		{
			return this.update(conn, closeConn, sql, params);
		}
		catch (SQLException e)
		{
			logger.error(e.getMessage(), e);
			return Constants.THROWABLE;
		}
	}
	
	public int update(Connection conn, boolean closeConn, String sql, Object... params) throws SQLException
	{
		if (conn == null)
		{
			throw new SQLException("Null connection");
		}
		if (sql == null)
		{
			if (closeConn)
			{
				DbUtils.closeQuietly(conn);
			}
			throw new SQLException("Null SQL statement");
		}
		PreparedStatement stmt = null;
		try
		{
			stmt = this.prepareStatement(conn, sql);
			this.fillStatement(stmt, params);
			return stmt.executeUpdate();
		}
		catch (SQLException e)
		{
			this.rethrow(e, sql, params);
			return Constants.THROWABLE;
		}
		finally
		{
			DbUtils.closeQuietly(stmt);
			if (closeConn)
			{
				DbUtils.closeQuietly(conn);
			}
		}
	}

	public int[] batchQuietly(Connection conn, boolean closeConn, String sql, Object[][] params)
	{
		try
		{
			return this.batch(conn, closeConn, sql, params);
		}
		catch (SQLException e)
		{
			logger.error(e.getMessage(), e);
			return null;
		}
	}
	
	public int[] batch(Connection conn, boolean closeConn, String sql, Object[][] params) throws SQLException
	{
		if (conn == null)
		{
			throw new SQLException("Null connection");
		}
		if (sql == null)
		{
			if (closeConn)
			{
				DbUtils.closeQuietly(conn);
			}
			throw new SQLException("Null SQL statement");
		}
		if (params == null)
		{
			if (closeConn)
			{
				DbUtils.closeQuietly(conn);
			}
			throw new SQLException("Null parameters. If parameters aren't need, pass an empty array.");
		}
		PreparedStatement stmt = null;
		try
		{
			stmt = this.prepareStatement(conn, sql);
			for (int i = 0; i < params.length; i++)
			{
				this.fillStatement(stmt, params[i]);
				stmt.addBatch();
			}
			return stmt.executeBatch();
		}
		catch (SQLException e)
		{
			this.rethrow(e, sql, (Object[]) params);
			return null;
		}
		finally
		{
			DbUtils.closeQuietly(stmt);
			if (closeConn)
			{
				DbUtils.closeQuietly(conn);
			}
		}
	}
	
	protected void fillStatement(PreparedStatement stmt, Object... params) throws SQLException
	{
		if (params == null)
		{
			return;
		}
		for (int i = 0; i < params.length; i++)
		{
			if (params[i] != null)
			{
				stmt.setObject(i + 1, params[i]);
			}
			else
			{
				stmt.setNull(i + 1, Types.VARCHAR);
			}
		}
	}

	protected PreparedStatement prepareStatement(Connection conn, String sql) throws SQLException
	{
		return conn.prepareStatement(sql);
	}

	protected void rethrow(SQLException cause, String sql, Object... params) throws SQLException
	{
		String causeMessage = cause.getMessage();
		if (causeMessage == null)
		{
			causeMessage = "";
		}
		StringBuffer msg = new StringBuffer(causeMessage);
		msg.append(" Query: ");
		msg.append(sql);
		msg.append(" Parameters: ");
		if (params == null)
		{
			msg.append("[]");
		}
		else
		{
			msg.append(Arrays.deepToString(params));
		}
		SQLException e = new SQLException(msg.toString(), cause.getSQLState(), cause.getErrorCode());
		e.setNextException(cause);
		throw e;
	}
}
